<template>
	<div class="system-oss-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="ossDialogFormRef" :model="state.ruleForm" size="default" label-width="90px" :rules="state.rules">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="配置key" prop="configKey">
							<el-input v-model="state.ruleForm.configKey" placeholder="请输入配置key" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="访问站点" prop="endpoint">
							<el-input v-model="state.ruleForm.endpoint" placeholder="请输入访问站点" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="桶名称" prop="bucketName">
							<el-input v-model="state.ruleForm.bucketName" placeholder="请输入桶名称" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="自定义域名" prop="domain">
							<el-input v-model="state.ruleForm.domain" placeholder="请输入自定义域名" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="accessKey" prop="accessKey">
							<el-input v-model="state.ruleForm.accessKey" placeholder="请输入accessKey" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="secretKey" prop="secretKey">
							<el-input v-model="state.ruleForm.secretKey" placeholder="请输入秘钥" show-password />
						</el-form-item>
					</el-col>

					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="前缀" prop="prefix">
							<el-input v-model="state.ruleForm.prefix" placeholder="请输入前缀" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="域" prop="region">
							<el-input v-model="state.ruleForm.region" placeholder="请输入域" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="是否HTTPS">
							<el-radio-group v-model="state.ruleForm.isHttps">
								<el-radio v-for="dict in sys_yes_no" :key="dict.value" :label="dict.value">{{ dict.label
								}}</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="桶权限类型">
							<el-radio-group v-model="state.ruleForm.accessPolicy">
								<el-radio label="0">private</el-radio>
								<el-radio label="1">public</el-radio>
								<el-radio label="2">custom</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="备注">
							<el-input v-model="state.ruleForm.remark" type="textarea" placeholder="请输入备注"
								clearable></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default" v-auth="'system:oss:edit'">{{
						state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="systemPostDialog">
import { reactive, ref } from 'vue';
import { ElMessage } from 'element-plus';
import { useOssConfig } from '/@/api/system/ossConfig';
import { useDict } from '/@/utils/dict';

const { sys_yes_no } = useDict("sys_yes_no");
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 定义变量内容
const ossDialogFormRef = ref();
const state = reactive({
	ruleForm: {
		ossConfigId: '',
		configKey: '',
		accessKey: '',
		secretKey: '',
		bucketName: '',
		prefix: '',
		endpoint: '',
		domain: '',
		isHttps: '',
		region: '',
		status: '',
		ext1: '',
		remark: null,
		accessPolicy: '',
	},
	rules: {
		configKey: [
			{ required: true, message: "configKey不能为空", trigger: "blur" },
		],
		accessKey: [
			{ required: true, message: "accessKey不能为空", trigger: "blur" },
			{
				min: 2,
				max: 200,
				message: "accessKey长度必须介于 2 和 100 之间",
				trigger: "blur",
			},
		],
		secretKey: [
			{ required: true, message: "secretKey不能为空", trigger: "blur" },
			{
				min: 2,
				max: 100,
				message: "secretKey长度必须介于 2 和 100 之间",
				trigger: "blur",
			},
		],
		bucketName: [
			{ required: true, message: "bucketName不能为空", trigger: "blur" },
			{
				min: 2,
				max: 100,
				message: "bucketName长度必须介于 2 和 100 之间",
				trigger: "blur",
			},
		],
		endpoint: [
			{ required: true, message: "endpoint不能为空", trigger: "blur" },
			{
				min: 2,
				max: 100,
				message: "endpoint名称长度必须介于 2 和 100 之间",
				trigger: "blur",
			},
		],
		accessPolicy: [
			{ required: true, message: "accessPolicy不能为空", trigger: "blur" }
		]
	},
	total: 0,
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});

// 打开弹窗
const openDialog = async (type: string, row: RowOssConfigType) => {
	state.dialog.type = type;
	if (type === 'edit') {
		state.dialog.title = '修改存储配置';
		state.dialog.submitTxt = '修 改';
		await getOssRuleForm(row);
	} else {
		state.dialog.title = '新增存储配置';
		state.dialog.submitTxt = '新 增';
		await RuleForm();
	}
	state.dialog.isShowDialog = true;
};

/**
 * 获取OSS配置表单
 */
const getOssRuleForm = async (row: RowOssConfigType) => {
	try {
		await useOssConfig().getOssConfig(row.ossConfigId).then((res) => {
			if (res.code === 200) {
				state.ruleForm = res.data;
			} else {
				ElMessage.error(res.msg);
			}
		});
	} catch (error) {
		return Promise.reject(error);
	}
}

// 提交
const onSubmit = async () => {
	try {
		await ossDialogFormRef.value.validate(async (valid: any, fields: any) => {
			if (valid) {
				if (state.dialog.type === 'add') {
					await addOssConfig();
				} else {
					await updateOssConfig();
				}
			} else {
				ElMessage.error('请检查输入项是否正确');
				return Promise.reject(fields);
			}
		});
	} catch (err) {
		Promise.reject(err);
	}
};

const addOssConfig = async () => {
	try {
		const res = await useOssConfig().addOssConfig({
			configkey: state.ruleForm.configKey,
			accesskey: state.ruleForm.accessKey,
			secretkey: state.ruleForm.secretKey,
			bucketname: state.ruleForm.bucketName,
			prefix: state.ruleForm.prefix,
			endpoint: state.ruleForm.endpoint,
			domain: state.ruleForm.domain,
			ishttps: state.ruleForm.isHttps,
			region: state.ruleForm.region,
			status: state.ruleForm.status,
			ext1: state.ruleForm.ext1,
			remark: state.ruleForm.remark,
			accesspolicy: state.ruleForm.accessPolicy,
		});
		if (res.code === 200) {
			ElMessage.success(res.msg);
			closeDialog();
			emit('refresh');
		} else {
			ElMessage.error(res.msg);
		}
	} catch (err) {
		Promise.reject(err);
	}
};

/**
 * 更新OssConfig
 * */
const updateOssConfig = async () => {
	try {
		const res = await useOssConfig().updateOssConfig(state.ruleForm);
		if (res.code === 200) {
			ElMessage.success(res.msg);
			closeDialog();
			emit('refresh');
		} else {
			ElMessage.error(res.msg);
		}
	} catch (err) {
		Promise.reject(err);
	}
};


// 初始化岗位数据
const RuleForm = () => {
	state.ruleForm = {
		ossConfigId: '',
		configKey: '',
		accessKey: '',
		secretKey: '',
		bucketName: '',
		prefix: '',
		endpoint: '',
		domain: '',
		isHttps: '',
		region: '',
		status: '',
		ext1: '',
		remark: null,
		accessPolicy: '',
	};
};
// 取消
const onCancel = () => {
	closeDialog();
	RuleForm();
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};// 暴露变量
defineExpose({
	openDialog,
});
</script>